﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Data;
using System.Data.SqlClient;

namespace Controller
{
    public class UoMDetailController : Controller.dbConnection.ConnectionClass
    {
        public string Save(UoMDetail Details)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_INSERTUPDATEUOMDETAIL";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = Details.ID;
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = Details.Description;
            command.Parameters.Add("@ISACTIVE", SqlDbType.VarChar).Value = Details.isActive;
            command.Parameters.Add("@UOMTYPE", SqlDbType.VarChar).Value = Details.UoMType;
            command.Parameters.Add("@LISTVALUE", SqlDbType.VarChar).Value = Details.ListValue;
            command.Parameters.Add("@UOMID", SqlDbType.VarChar).Value = Details.UoM.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = Details.LastModifiedBy;
            command.Parameters.Add("@CREATEBY", SqlDbType.VarChar).Value = Details.CreateBy;

            return ExecuteNonQuery(command);
        }

        public string Delete(UoMDetail Details)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_DELETEUoMDetail";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = Details.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = Details.LastModifiedBy;

            return ExecuteNonQuery(command);
        }

        public string BeforeDelete(UoMDetail Details)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_CheckBeforeDeleteUomDetailKPI";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = Details.ID;
            //command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = Details.LastModifiedBy;

            return GetSingleData(command);
        }

        public IList<UoMDetail> GetAll(string sSearch, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllUoMDetails";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;

            DataSet dx = GetTable(command);

            List<UoMDetail> Details = new List<UoMDetail>();

            switch (SortDirection)
            {
                case "asc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description ASC";
                            break;
                        case "UoM":
                            dx.Tables[0].DefaultView.Sort = "UoMID ASC";
                            break;
                        case "ListValue":
                            dx.Tables[0].DefaultView.Sort = "ListValue ASC";
                            break;
                        case "UoMType":
                            dx.Tables[0].DefaultView.Sort = "UoMType ASC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate ASC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy ASC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate ASC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy ASC";
                            break;
                    }
                    break;
                case "desc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description DESC";
                            break;
                        case "UoM":
                            dx.Tables[0].DefaultView.Sort = "UoMID DESC";
                            break;
                        case "ListValue":
                            dx.Tables[0].DefaultView.Sort = "ListValue DESC";
                            break;
                        case "UoMType":
                            dx.Tables[0].DefaultView.Sort = "UoMType DESC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate DESC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy DESC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate DESC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy DESC";
                            break;
                    }
                    break;
            }

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        UoMDetail p = new UoMDetail();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.UoMType = dt.Rows[i]["UoMType"].ToString();
                        p.ListValue = dt.Rows[i]["ListValue"].ToString();
                        p.UoM = new UoMController().GetUoMByID(Convert.ToInt16(dt.Rows[i]["UoMID"].ToString()));
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        Details.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return Details.ToList();
        }

        public IList<UoMDetail> GetAllUoMDetailsByUoMID(string sSearch, int UoMID, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllUoMDetailsByUoMID";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;
            command.Parameters.Add("@UoMID", SqlDbType.Int).Value = UoMID;

            DataSet dx = GetTable(command);

            List<UoMDetail> Details = new List<UoMDetail>();

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        UoMDetail p = new UoMDetail();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.UoMType = dt.Rows[i]["UoMType"].ToString();
                        p.ListValue = dt.Rows[i]["ListValue"].ToString();
                        p.UoM = new UoMController().GetUoMByID(Convert.ToInt16(dt.Rows[i]["UoMID"].ToString()));
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        Details.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return Details.ToList();
        }

        public UoMDetail GetUoMDetailByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GETUOMDETAILBYID";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            UoMDetail UoMDetails = null;

            if (Data != null && Data.Count > 0)
            {
                UoMDetails = new UoMDetail();
                UoMDetails.ID = Convert.ToInt16(Data[0]);
                UoMDetails.Description = Data[1].ToString();
                UoMDetails.isActive = Convert.ToBoolean(Data[2]);
                UoMDetails.UoMType = Data[3].ToString();
                UoMDetails.ListValue = Data[4].ToString();
                UoMDetails.UoM = new UoMController().GetUoMByID(Convert.ToInt16(Data[5]));
                UoMDetails.LastModifiedDate = Data[6] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[6].ToString());
                UoMDetails.LastModifiedBy = Data[7].ToString();
                UoMDetails.CreateDate = Data[8] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[8].ToString());
                UoMDetails.CreateBy = Data[9].ToString();
            }

            return UoMDetails;
        }

        public UoMDetail GetUoMDetailByDescription(string Description, int UOMID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GETUOMDETAILBYDESCRIPTION";
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = Description;
            command.Parameters.Add("@UOMID", SqlDbType.Int).Value = UOMID;

            IList<string> Data = GetSingleRow(command);
            UoMDetail UoMDetails = null;

            if (Data != null && Data.Count > 0)
            {
                UoMDetails = new UoMDetail();
                UoMDetails.ID = Convert.ToInt16(Data[0]);
                UoMDetails.Description = Data[1].ToString();
                UoMDetails.isActive = Convert.ToBoolean(Data[2]);
                UoMDetails.UoMType = Data[3].ToString();
                UoMDetails.ListValue = Data[4].ToString();
                UoMDetails.UoM = new UoMController().GetUoMByID(Convert.ToInt16(Data[5]));
                UoMDetails.LastModifiedDate = Data[6] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[6].ToString());
                UoMDetails.LastModifiedBy = Data[7].ToString();
                UoMDetails.CreateDate = Data[8] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[8].ToString());
                UoMDetails.CreateBy = Data[9].ToString();
            }

            return UoMDetails;
        }
    }
}